<?php
/**
 * 菜单管理
 *
 * @category     Shuxun Cms
 * @date         2015年10月18日
 * @author       Shuxun Team <kaiping.jiang@scshux.com>
 * @file         MenusController.php
 */
namespace Scshux\Backend\Controllers;
use Scshux\Mvc\SModel;
use Scshux\Mvc\SQuery;
use Scshux\Utils\Tree;
use Scshux\Utils\Time;
use Scshux\Utils\Uploader;
class MenusController extends  ControllerBase
{

	/**
	 * 菜单列表
	 */
	public function   indexAction()
	{
		$is_ajax  = $this->request->get('is_ajax','int',0);
		$dataList = $this->_getCategoryDataList();
		$this->view->setVar('dataList', $dataList);
		if($is_ajax){
			$this->view->setVar('full_page',0);
			$this->view->start();
			$this->view->render('menus','index');
			$this->view->finish();
			$content = $this->view->getContent();
			$data = array(
					'content' => $content,
					'filter'  => $dataList->filter,
					'totalPage' => $dataList->totalPage,
					'recordCount'=>$dataList->totalSize,
					'currentPage'=>$dataList->currentPage,
					'extended'   =>$dataList->extended
			);
			$this->sendResult(true,'',$data);
		}else{
			$this->view->setVar('full_page',1);
		}

	}

	/**
	 * 帮助分类编辑
	 */
	public function   editAction()
	{
		$id = $this->request->get('id','int',0);
		if($this->request->isPost()){
			$categoryModel = new SModel('menus');
			//加入语言
			$_POST['site_id'] = $this->getCore()->getSiteID();
			$postData = $_POST;
			
			if($id>0){
				$categoryModel->setData($postData);
				$categoryModel->update('id='.$id);
			}else{
				//处理id为空的情况；
				unset($postData['id']);
				$categoryModel->setData($postData);
				$categoryModel->add();
			}
			
			return $this->redirect('menus/index');
		}else{
			if($id>0){
				$categoryModel = new SModel('menus');
				$item = $categoryModel->getObj('id='.$id);
				$this->view->setVar('item', $item);
				$this->view->setVar('categoryHtml', $this->getCategoriesHtml($item->parent_id,$id));
			}else{
				$this->view->setVar('categoryHtml', $this->getCategoriesHtml(0));
			}
		}
	}

	/**
	 * 删除分类
	 */
	public  function  delAction()
	{
		$id = $this->request->get('id','int',0);
		if($id>0){
			$categoryModel = new SModel('menus');
			$categoryModel->del("id='{$id}' or parent_id='{$id}'");
		}
		return $this->redirect('menus/index');
	}


	/**
	 * 分类列表数据
	 */
	protected   function  _getCategoryDataList()
	{
		$filter = array();
		$items = $this->getCategoriesTree();
		foreach ($items as $key => $item)
		{
			$items[$key] = (object)$item;
		}
		$dataList = new \stdClass();
		$dataList->totalPage = 1;
		$dataList->currentPage = 1;
		$dataList->items = $items;
		$dataList->totalSize = count($items);
		$dataList->filter = $filter;
		return  $dataList;
	}

	/**
	 * 获取所有分类html
	 */
	protected  function  getCategoriesHtml($id = 0,$outId=0)
	{
		$categoryModel = new SModel('menus');
		$categoryModel->setFetchMode(\PDO::FETCH_ASSOC);
		
		//新增语言过滤
		$site_id = $this->getCore()->getSiteID();
		$where = 'site_id = '.$site_id;
		
		$items = $categoryModel->query($where,'*','sort desc,id','DESC');
	    $treeObj = new Tree($items);
	    return  $treeObj->getHtmlOptions($id,$outId);
	}


	/**
	 * 获取所有分类树
	 */
	protected  function  getCategoriesTree($id = 0)
	{
		$categoryModel = new SModel('menus');
		$categoryModel->setFetchMode(\PDO::FETCH_ASSOC);
		
		//新增语言过滤
		$site_id = $this->getCore()->getSiteID();
		$where = 'site_id = '.$site_id;
		
		$items = $categoryModel->query($where,'*','sort desc,id','DESC');
		$treeObj = new Tree($items);
		return  $treeObj->unlimitedForLevel('&nbsp;&nbsp;',$id);
	}
}